// pages/parking/parking.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuList: [
      {
        id: 1,
        name: '停车记录',
        icon: '/assets/icons/record.png',
        url: '/pages/parking/record/list'
      },
      {
        id: 2,
        name: '电子发票',
        icon: '/assets/icons/invoice.png',
        url: '/pages/parking/invoice/apply'
      },
      {
        id: 3,
        name: '月卡办理',
        icon: '/assets/icons/card.png',
        url: '/pages/parking/card/apply'
      },
      {
        id: 4,
        name: '我的车辆',
        icon: '/assets/icons/car.png',
        url: '/pages/parking/car/list'
      }
    ],
    parkingList: [
      {
        id: 1,
        name: '中心广场停车场',
        address: '武汉市江汉区解放大道1号',
        availableSpots: 25,
        distance: '1.2km',
        latitude: 30.593116,
        longitude: 114.305649
      },
      {
        id: 2,
        name: '商业街地下停车场',
        address: '武汉市江汉区解放大道2号',
        availableSpots: 8,
        distance: '1.5km',
        latitude: 30.594116,
        longitude: 114.306649
      },
      {
        id: 3,
        name: '汉街地下停车场',
        address: '武汉市武昌区楚河汉街',
        availableSpots: 45,
        distance: '2.1km',
        latitude: 30.552816,
        longitude: 114.320749
      },
      {
        id: 4,
        name: '光谷广场停车场',
        address: '武汉市洪山区光谷广场',
        availableSpots: 32,
        distance: '3.5km',
        latitude: 30.508359,
        longitude: 114.398559
      },
      {
        id: 5,
        name: '武汉天地停车场',
        address: '武汉市江岸区武汉天地',
        availableSpots: 15,
        distance: '2.8km',
        latitude: 30.596116,
        longitude: 114.312649
      },
      {
        id: 6,
        name: '测试停车场1',
        address: '武汉市测试地址1',
        availableSpots: 20,
        distance: '3.0km',
        latitude: 30.596116,
        longitude: 114.312649
      },
      {
        id: 7,
        name: '测试停车场2',
        address: '武汉市测试地址2',
        availableSpots: 5,
        distance: '3.2km',
        latitude: 30.596116,
        longitude: 114.312649
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getNearbyParkings()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 获取附近停车场
  getNearbyParkings() {
    // TODO: 调用获取附近停车场API
  },

  // 搜索停车场
  onSearch() {
    wx.navigateTo({
      url: '/pages/parking/search/search'
    })
  },

  // 点击功能菜单
  onMenuClick(e) {
    const { id } = e.currentTarget.dataset
    const menu = this.data.menuList.find(item => item.id === id)
    if (menu) {
      wx.navigateTo({
        url: menu.url
      })
    }
  },

  // 点击停车场
  onParkingClick(e) {
    const { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/parking/detail/detail?id=${id}`
    })
  },

  // 导航
  onNavigation(e) {
    const { item } = e.currentTarget.dataset
    wx.openLocation({
      latitude: item.latitude,
      longitude: item.longitude,
      name: item.name,
      address: item.address
    })
  }
})